package com.tucc.hello.leetcode.no1_400.no1_200.no1_20.no1_10.no8;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();

    }

    public int myAtoi(String s) {
        if (s != null && s.length() > 0) {
            String regex = "^\\s*(?<number>([-+]?\\d+))";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(s);
            if (matcher.find()) {
                String numstr = matcher.group("number");
                numstr = numstr.trim();
                try {
                    if (numstr != null) {
                        int num = Integer.parseInt(numstr.trim());
                        return num;
                    }
                } catch (Exception e) {
                    if(numstr.charAt(0) == '-'){
                        return Integer.MIN_VALUE;
                    }
                    return Integer.MAX_VALUE;
                }
            }
        }
        return 0;
    }
}
